package com.vanysoft.business.sys.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.vanysoft.business.sys.entity.Menu;
import com.vanysoft.business.sys.entity.Org;
import com.vanysoft.business.sys.entity.Role;
import com.vanysoft.business.sys.entity.UserInfo;
import com.vanysoft.business.sys.service.MenuService;
import com.vanysoft.business.sys.service.RoleService;
import com.vanysoft.business.sys.service.UserInfoService;
import com.vanysoft.framework.base.action.BaseController;
import com.vanysoft.framework.config.database.PageInfo;
import com.vanysoft.framework.config.mvc.SessionUser;
import com.vanysoft.framework.exception.VanySoftBaseException;
import com.vanysoft.framework.messagesource.MessageCodeConstant;
import com.vanysoft.framework.utils.JSONResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qi_ma
 */
@RequestMapping("/sys/role")
@Controller
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;

    @Resource
    private MenuService menuService;

    @Resource
    private UserInfoService userInfoService;

    @RequestMapping("role")
    public String role() {
        return "sys/role";
    }

    /**
     * Function Name               getRoleInfo
     *
     * @return
     * @throws NumberFormatException
     * @throws VanySoftBaseException
     * @description 查询组织机构下的人员
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月24日 下午2:15:32           SharpClaw_x      Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/getRoleInfo", method = {RequestMethod.GET})
    @ResponseBody
    public JSONResponse getRoleInfo(PageInfo<Role> page) throws VanySoftBaseException {
        page = roleService.selectAll(page);
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, page);
    }

    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    @ResponseBody
    public JSONResponse list() {
        return new JSONResponse(MessageCodeConstant.RTN_CODE_SUCCESS, roleService.selectAll());
    }

    /**
     * Function Name               addRole
     *
     * @return
     * @throws VanySoftBaseException
     * @description 角色添加
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月1日 下午3:44:16           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/addRole", method = {RequestMethod.POST})
    @ResponseBody
    public JSONResponse addRole(@SessionUser UserInfo userInfo, Role role) throws VanySoftBaseException {
        role.setOwner(userInfo.getId());
        role.setAdmin(false);
        role.setDefault(false);
        boolean flag = roleService.insert(role);
        return new JSONResponse(flag ? MessageCodeConstant.RTN_CODE_SUCCESS : MessageCodeConstant.RTN_CODE_UPDATE_FAIL);

    }

    /**
     * Function Name               modifyRoleInfo
     *
     * @return
     * @throws VanySoftBaseException
     * @description 修改
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月1日 下午4:25:57           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/modifyRoleInfo", method = {RequestMethod.POST})
    @ResponseBody
    public JSONResponse modifyRoleInfo(@SessionUser UserInfo userInfo, Role role) throws VanySoftBaseException {
        role.setOwner(userInfo.getId());
        role.setAdmin(false);
        role.setDefault(false);
        boolean flag = roleService.updateById(role);
        return new JSONResponse(flag ? MessageCodeConstant.RTN_CODE_SUCCESS : MessageCodeConstant.RTN_CODE_UPDATE_FAIL);
    }

    /**
     * Function Name               deleteRole
     *
     * @return
     * @throws VanySoftBaseException
     * @description 角色批量删除
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月1日 下午4:44:59           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/deleteRole", method = {RequestMethod.POST})
    @ResponseBody
    public JSONResponse deleteRole(String[] ids) throws VanySoftBaseException {
        if (ids == null || ids.length == 0) {
            throw new VanySoftBaseException(MessageCodeConstant.RTN_CODE_UNKNOW_ERROR, "roleCodes is null");
        }
        boolean flag = roleService.deleteByIds(ids);
        return new JSONResponse(flag ? MessageCodeConstant.RTN_CODE_SUCCESS : MessageCodeConstant.RTN_CODE_UPDATE_FAIL);

    }

    /**
     * Function Name               loadRoleUserInfo
     *
     * @return
     * @throws VanySoftBaseException
     * @description 加载角色人员
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月1日 下午4:57:01           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/loadRoleUserInfo", method = {RequestMethod.GET})
    @ResponseBody
    public JSONResponse loadRoleUserInfo(Page<UserInfo> page) throws VanySoftBaseException {
        page = userInfoService.selectAll(page);
        return new JSONResponse(page);
    }

    /**
     * Function Name               modifyRoleUser
     *
     * @return
     * @throws VanySoftBaseException
     * @description 角色人员添加
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月1日 下午6:37:29           SharpClaw_x                    Initial
     * *********************************************************************
     */

    @RequestMapping(value = "/modifyRoleUser", method = {RequestMethod.POST})
    @ResponseBody
    public JSONResponse modifyRoleUser(String roleId, String[] userIds) throws VanySoftBaseException {
        if (userIds == null || userIds.length == 0) {
            throw new VanySoftBaseException(MessageCodeConstant.RTN_CODE_UNKNOW_ERROR, "userIds is null");
        }
        boolean flag = roleService.addRoleUsers(roleId, userIds);
        return new JSONResponse(flag ? MessageCodeConstant.RTN_CODE_SUCCESS : MessageCodeConstant.RTN_CODE_UPDATE_FAIL);

    }

    /**
     * Function Name               deleteRoleUser
     *
     * @return
     * @throws VanySoftBaseException
     * @description 移除角色人员
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月1日 下午6:49:53           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/deleteRoleUser", method = {RequestMethod.POST})
    @ResponseBody
    public JSONResponse deleteRoleUser(String roleId, String[] userIds) throws VanySoftBaseException {
        boolean flag = roleService.deleteByRoleIdAndUserIds(roleId, userIds);
        return new JSONResponse(flag ? MessageCodeConstant.RTN_CODE_SUCCESS : MessageCodeConstant.RTN_CODE_UPDATE_FAIL);

    }


    /**
     * Function Name               loadRoleInfo
     *
     * @return
     * @throws VanySoftBaseException
     * @description 加载角色
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年5月31日 下午6:43:58           SharpClaw_x                    Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/loadRoleInfo", method = {RequestMethod.GET})
    @ResponseBody
    public JSONResponse loadRoleInfo(@SessionUser UserInfo userInfo, String roleName) throws VanySoftBaseException {
        Map<String, Object> condition = new HashMap<>();
        condition.put("roleName", roleName);
        if (!userInfo.isAdmin()) {
            condition.put("owner", userInfo.getId());
        }
        List<Role> roles = roleService.selectAll(condition);
        return new JSONResponse(roles);

    }

    /**
     * Function Name               loadFunctionByRoleCode
     *
     * @return
     * @throws VanySoftBaseException
     * @description 根据角色代码查询已分配功能权限和未分配功能权限
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月7日 上午11:43:58          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/loadFunctionByRoleCode", method = {RequestMethod.GET})
    @ResponseBody
    public JSONResponse loadMenuListByRoleId(@SessionUser UserInfo userInfo, String roleId) throws VanySoftBaseException {
        //当前登录用户的菜单集合
        List<Menu> ownMenuList = buildMenuTree(userInfo.getMenus());
        List<String> allotIds = menuService.selectIdsByRoleId(roleId);
        //ownFunction,allotFunction
        Map<String, Object> map = new HashMap<>();
        map.put("ownMenuList", ownMenuList);
        map.put("allotIds", allotIds);
        return new JSONResponse(map);
    }

    /**
     * Function Name               modifyRoleFunctionAllotAndRemove
     *
     * @throws VanySoftBaseException
     * @description 角色功能分配与移除
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月7日 上午11:45:22          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/modifyRoleFunctionAllotAndRemove", method = {RequestMethod.POST})
    @ResponseBody
    public JSONResponse modifyRoleFunctionAllotAndRemove(String roleId, String[] menuIds) throws VanySoftBaseException {
        boolean flag = menuService.saveByRoleIdAndMenuIds(roleId, menuIds);
        return new JSONResponse(flag ? MessageCodeConstant.RTN_CODE_SUCCESS : MessageCodeConstant.RTN_CODE_UPDATE_FAIL);

    }


    /**
     * Function Name               loadDataByRoleCode
     *
     * @throws VanySoftBaseException
     * @description 根据角色代码查询已分配数据权限和未分配数据权限
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月7日 下午3:05:10          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/loadDataByRoleCode", method = {RequestMethod.GET})
    @ResponseBody
    public JSONResponse loadDataByRoleCode(@SessionUser UserInfo userInfo, String roleId) throws VanySoftBaseException {
        //当前登录用户的菜单集合
        List<Org> ownOrgList = buildOrgTree(userInfo.getOrgs());
        Role role = roleService.selectById(roleId);
        String[] allotCodes = role.getOrgCodes();
        //ownAllotData,allotData
        Map<String, Object> map = new HashMap<>();
        map.put("ownOrgList", ownOrgList);
        map.put("allotCodes", allotCodes);
        return new JSONResponse(map);
    }

    /**
     * Function Name               modifyRoleDataAllotAndRemove
     *
     * @throws VanySoftBaseException
     * @description 根据角色编号, 数据权限代码字符串 进行数据权限的分配与移除
     * Modify History:              Date             Programmer       Notes
     * ---------        ---------------  ---------
     * 2017年6月7日 下午5:22:29          SharpClaw_x                     Initial
     * *********************************************************************
     */
    @RequestMapping(value = "/modifyRoleDataAllotAndRemove", method = {RequestMethod.POST})
    @ResponseBody
    public JSONResponse modifyRoleDataAllotAndRemove(String roleId, String[] orgCodes) throws VanySoftBaseException {
        boolean flag = roleService.updateOrgCodes(roleId, orgCodes);
        return new JSONResponse(flag ? MessageCodeConstant.RTN_CODE_SUCCESS : MessageCodeConstant.RTN_CODE_UPDATE_FAIL);

    }

}
